﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Security.Cryptography;

namespace ConsoleApp.Security
{
    public sealed class AesProvider
    {
        static Rijndael aes = null;

        static AesProvider()
        {
            aes = Rijndael.Create();
            aes.Mode = CipherMode.ECB;
            aes.Padding = PaddingMode.PKCS7;
        }

        /// <summary>
        /// AES加密
        /// </summary>
        /// <param name="data"></param>
        /// <returns></returns>
        public static string Encrypt(string data, string key = "4BB97A76D2496F80")
        {
            aes.Key = Encoding.UTF8.GetBytes(key);
            byte[] bData = Encoding.UTF8.GetBytes(data);
            ICryptoTransform iCryptoTransform = aes.CreateEncryptor();
            byte[] bResult = iCryptoTransform.TransformFinalBlock(bData, 0, bData.Length);
            return Convert.ToBase64String(bResult);
        }

        /// <summary>
        /// ASE解密
        /// </summary>
        /// <param name="data"></param>
        /// <returns></returns>
        public static string Decrypt(string data, string key = "4BB97A76D2496F80")
        {
            aes.Key = Encoding.UTF8.GetBytes(key);
            byte[] bData = Convert.FromBase64String(data);
            ICryptoTransform iCryptoTransform = aes.CreateDecryptor();
            byte[] bResult = iCryptoTransform.TransformFinalBlock(bData, 0, bData.Length);
            return Encoding.UTF8.GetString(bResult);
        }
    }
}